<template>
    <el-card shadow="never" class="trend_card">
        <div>
            <div class="title">
                <p>客户趋势分析</p>
                <el-button :disabled="data.dateList.length == 0" @click="export2Img(myChartLineRef, '趋势图')"
                    >导出图片</el-button
                >
            </div>
        </div>
        <div v-if="data.dateList.length" id="echarts" ref="myChartLineRef"></div>
        <div v-else class="none_data">
            <img class="no_img" src="@/assets/noData.png" alt="" />
            <div>暂无数据</div>
        </div>
    </el-card>
</template>

<script setup lang="ts">
import { ref, inject, nextTick, watch } from 'vue';
import { export2Img } from '@/utils/utils';
const data = ref({
    nameList: [],
    dateList: [],
    dataList: []
});
const echarts: any = inject('ec');
const props = defineProps(['info']);
const myChartLineRef = ref(null);
const getEchartsLine = () => {
    const myChartLine = echarts?.init(myChartLineRef.value);
    const option = {
        color: [
            '#4B7FE8',
            '#A6ED71',
            '#FF9D23',
            '#32D1AD',
            '#FF4412',
            '#FF9D23',
            '#32D1AD',
            '#FF4412',
            '#FF9D23',
            '#32D1AD',
            '#FF4412'
        ],
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            x: 'center',
            y: 'bottom',
            bottom: 30,
            padding: [0, 0, 10, 0],
            data: data.value.nameList
        },
        grid: {
            top: 50,
            bottom: 60,
            left: 32,
            right: 40,
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: data.value.dateList,
            boundaryGap: false,
            offset: 3
        },
        yAxis: {
            type: 'value',
            name: '人',
            nameGap: 10, // y轴name与横纵坐标轴线的间距
            nameTextStyle: {
                padding: [0, 35, 10, 0] // 加上padding可以调整其位置
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                color: '#969696'
            },
            splitLine: {
                lineStyle: {
                    type: 'line',
                    color: '#e8e8e8' // 分割线颜色
                }
            }
        },
        series: data.value.dataList
    };
    myChartLine.setOption(option);
    window.addEventListener('resize', function () {
        if (myChartLine) {
            myChartLine.resize();
        }
    });
};
watch(
    () => props.info,
    val => {
        data.value.nameList = val.name_list;
        data.value.dateList = val.date_list;
        data.value.dataList = val.name_list.map((item: any, index: any) => {
            return {
                name: item,
                type: 'line',
                data: val.data_list[index],
                smooth: true,
                symbol: 'circle'
            };
        });
        nextTick(() => {
            getEchartsLine();
        });
    }
);
</script>

<style lang="scss" scoped>
.trend_card {
    margin-top: 16px;

    .title {
        padding-bottom: 10px;
        display: flex;
        font-size: 16px;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #eaeaea;
    }

    .btn_box {
        display: flex;
        justify-content: space-between;
        padding: 16px 0;
    }

    #echarts {
        display: flex;
        justify-content: center;
        height: 500px;
    }

    .circle_box {
        display: flex;
        margin-bottom: 16px;

        .circle {
            flex: 1;
            padding: 20px 26px;
            margin-right: 20px;
            background: #fff;
            box-shadow: 0 4px 4px 0 rgb(240 240 240 / 50%);
            border: 1px solid #eaeaea;

            .circle_title {
                display: flex;
                justify-content: space-between;
                align-items: center;
                padding-bottom: 8px;
                border-bottom: 1px solid #eaeaea;
            }
        }

        .margin {
            margin-right: 0;
        }
    }

    .none_data {
        margin: 100px 0;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: #909399;

        img {
            transform: translateX(15%);
        }
    }
}
</style>
